package com.jlc.book.shop.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jlc.book.shop.action.AddBookAction;
import com.jlc.book.shop.action.AddBookToCardAction;
import com.jlc.book.shop.action.AllOrdersAction;
import com.jlc.book.shop.action.CancleEditInfoAction;
import com.jlc.book.shop.action.ChangePasswordAction;
import com.jlc.book.shop.action.DeleteBookAction;
import com.jlc.book.shop.action.EditInfoAction;
import com.jlc.book.shop.action.ForgetPasswordAction;
import com.jlc.book.shop.action.LoginAction;
import com.jlc.book.shop.action.LogoutAction;
import com.jlc.book.shop.action.NextAction;
import com.jlc.book.shop.action.PlaceOrderAction;
import com.jlc.book.shop.action.PreviousAction;
import com.jlc.book.shop.action.RegisterAction;
import com.jlc.book.shop.action.RemoveFromCartAction;
import com.jlc.book.shop.action.SearchBookAction;
import com.jlc.book.shop.action.ShowOrderInfoAction;
import com.jlc.book.shop.action.ShowUserInfoAction;
import com.jlc.book.shop.action.SortBookInfoAction;
import com.jlc.book.shop.action.UpdateInfoAction;
import com.jlc.book.shop.action.UpdateOrderStatusAction;
import com.jlc.book.shop.action.UserOrderStatusAction;

public class JLCBookShopServlet extends HttpServlet{
	
	LoginAction loginAction=null;
	RegisterAction registerAction=null;
	ForgetPasswordAction forgetPasswordAction=null;
	LogoutAction logoutAction=null;
	ChangePasswordAction changePasswordAction=null;
	AddBookAction addBookAction=null;
	SearchBookAction searchBookAction=null;
	NextAction nextAction=null;
	PreviousAction previousAction=null;
    DeleteBookAction deleteBookAction=null;
    SortBookInfoAction sortBookInfoAction=null;
    AddBookToCardAction addBookToCardAction=null;
    PlaceOrderAction placeOrderAction=null;
    RemoveFromCartAction removeFromCartAction=null;
    AllOrdersAction allOrdersAction=null;
    ShowOrderInfoAction showOrderInfoAction=null;
    ShowUserInfoAction showUserInfoAction=null;
    UserOrderStatusAction userOrderStatusAction=null;
    UpdateOrderStatusAction updateOrderStatusAction=null;
    EditInfoAction editInfoAction=null;
    UpdateInfoAction updateInfoAction=null;
    CancleEditInfoAction cancleEditInfoAction=null;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		
		super.init(config);
		
		loginAction=new LoginAction();
		
		registerAction=new RegisterAction();
		forgetPasswordAction=new ForgetPasswordAction();
		logoutAction=new LogoutAction();
		changePasswordAction=new ChangePasswordAction();
		addBookAction=new AddBookAction();
		searchBookAction=new SearchBookAction();
		nextAction=new NextAction();
		previousAction=new PreviousAction();
		deleteBookAction=new DeleteBookAction();
		sortBookInfoAction=new SortBookInfoAction();
		addBookToCardAction=new AddBookToCardAction();
		placeOrderAction=new PlaceOrderAction();
		removeFromCartAction=new RemoveFromCartAction();
		allOrdersAction=new AllOrdersAction();
		showOrderInfoAction=new ShowOrderInfoAction();
		showUserInfoAction =new ShowUserInfoAction();
		userOrderStatusAction=new UserOrderStatusAction();
		updateOrderStatusAction=new UpdateOrderStatusAction();
		editInfoAction=new EditInfoAction();
		updateInfoAction=new UpdateInfoAction();
		cancleEditInfoAction=new CancleEditInfoAction();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String uri=request.getRequestURI();
		String page=" ";
		if(uri.endsWith("login.jlc"))
		{
			page=loginAction.verifyUser(request,response);
		}
		else if(uri.endsWith("register.jlc"))
		{
			page=registerAction.registerStudent(request,response);
		}
		else if(uri.endsWith("forgetPassword.jlc"))
		{
			page=forgetPasswordAction.searchPassword(request,response);
		}
		
		else if(uri.endsWith("logout.jlc"))
		{
			page=logoutAction.logout(request,response);
		}
		

		else if(uri.endsWith("changePassword.jlc"))
		{
			page=changePasswordAction.changePassword(request,response);
		}

		else if(uri.endsWith("addBook.jlc"))
		{
			page=addBookAction.addBookInfo(request,response);
		}
		
		else if(uri.endsWith("searchBook.jlc"))
		{
			page=searchBookAction.searchBookInfo(request,response);
		}
		
		else if(uri.endsWith("next.jlc"))
		{
			page=nextAction.searchBookInfo(request,response);
		}
		
		else if(uri.endsWith("previous.jlc"))
		{
			page=previousAction.searchBookInfo(request,response);
		}

		else if(uri.endsWith("delete.jlc"))
		{
			page=deleteBookAction.deleteBook(request,response);
		}
		else if(uri.endsWith("sortInfo.jlc"))
		{
			page=sortBookInfoAction.sortBookInfo(request,response);
		}
		else if(uri.endsWith("addtocart.jlc"))
		{
			page=addBookToCardAction.addBookToCart(request,response);
		}
		
		else if(uri.endsWith("placeorder.jlc"))
		{
			page=placeOrderAction.placeOrder(request,response);
		}
		
		else if(uri.endsWith("removefromcart.jlc"))
		{
			page=removeFromCartAction.removeFromCart(request,response);
		}
		
		else if(uri.endsWith("allorders.jlc"))
		{
			page=allOrdersAction.getAllOrders(request,response);
		}
		
		else if(uri.endsWith("showorderinfo.jlc"))
		{
			page=showOrderInfoAction.getOrderInfo(request,response);
		}
		
		else if(uri.endsWith("showuserinfo.jlc"))
		{
			page=showUserInfoAction.getUserInfo(request,response);
		}
		
		else if(uri.endsWith("userorderstatus.jlc"))
		{
			page=userOrderStatusAction.getUserOrderStatus(request, response);
		}
		
		else if(uri.endsWith("updatestatus.jlc"))
		{
			page=updateOrderStatusAction.updateOrderStatus(request, response);
		}
		
		else if(uri.endsWith("editinfo.jlc"))
		{
			page=editInfoAction.makeEditable(request, response);
		}

		else if(uri.endsWith("updateinfo.jlc"))
		{
			page=updateInfoAction.updateUserInfo(request, response);
		}
		else if(uri.endsWith("cancleedit.jlc"))
		{
			page=cancleEditInfoAction.cancleEditInfo(request, response);
		}
		
		request.getRequestDispatcher(page).forward(request, response);
		
	}

}
